package com.dq.service.sys.user.centre.impl;

import com.dq.domain.subscribe.Subscribe;
import com.dq.domain.sys.user.user.BossUser;
import com.dq.domain.sys.user.user.centre.UserScheduling;
import com.dq.exception.customException.AdminException;
import com.dq.repository.BaseRepositories;
import com.dq.service.sys.user.centre.UserSchedulingService;
import com.dq.utils.TimeUtil;
import com.dq.vo.Vo;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author luchangtong
 * @Date 2020/5/9 17:35
 */
@Service
public class UserSchedulingServiceImpl extends BaseRepositories implements UserSchedulingService {
    @Override
    public List<UserScheduling.Vo> list(Long id, Long year, Long month,Long day) throws AdminException {
        BossUser bossUser = dao.get(BossUser.class,id);
        List<UserScheduling> userSchedulings = new ArrayList<>();
        if(day == null){
            userSchedulings = userSchedulingRepository.findByDeletedFalseAndBossUserAndYearAndMonth(bossUser,year,month);
        }else {
            userSchedulings = userSchedulingRepository.findByDeletedFalseAndBossUserAndYearAndMonthAndDay(bossUser, year, month, day);
        }
        List<UserScheduling.Vo> toList = new ArrayList<>();
        List<Subscribe.SubscribeStatus> subscribeStatuses = new ArrayList<>();
        subscribeStatuses.add(Subscribe.SubscribeStatus.confirmed);
        subscribeStatuses.add(Subscribe.SubscribeStatus.cancelled);
        subscribeStatuses.add(Subscribe.SubscribeStatus.stayPaid);
        subscribeStatuses.add(Subscribe.SubscribeStatus.paid);
        subscribeStatuses.add(Subscribe.SubscribeStatus.signature);
        subscribeStatuses.add(Subscribe.SubscribeStatus.serviced);
        userSchedulings.forEach(userScheduling -> {
            UserScheduling.Vo vo = (UserScheduling.Vo) userScheduling.toVO();
            Long startTime =TimeUtil.getDayStartTime( userScheduling.getYear().toString()+"-"+userScheduling.getMonth().toString()+"-"+userScheduling.getDay());
            Long endTime =TimeUtil.getDayEndTime( userScheduling.getYear().toString()+"-"+userScheduling.getMonth().toString()+"-"+userScheduling.getDay());
            List<Subscribe> subscribes = subscribeRepository.findByTechnicianUserAndDeletedFalseAndBeginTimeGreaterThanAndEndTimeLessThanAndSubscribeStatusIn(userScheduling.getBossUser(),startTime,endTime,subscribeStatuses);
            String subscribeTime = "";
            for (Subscribe subscribe:subscribes) {
                subscribeTime = subscribeTime+TimeUtil.longToString(subscribe.getBeginTime(),TimeUtil.HHMM)+"-"+TimeUtil.longToString(subscribe.getEndTime(),TimeUtil.HHMM)+",";
            }
            vo.setSubsribeTime(subscribeTime);
            toList.add(vo);
        });
        return toList;
    }

    @Override
    public UserScheduling.Vo listByDay(Long id, Long year, Long month, Long day) throws AdminException {
        UserScheduling userScheduling = userSchedulingRepository.findByDeletedFalseAndBossUserIdAndYearAndMonthAndDay(id,year,month,day);
        UserScheduling.Vo vo = new UserScheduling.Vo();
        if(userScheduling != null){
            vo = (UserScheduling.Vo) userScheduling.toVO();
        }
        return vo;
    }

    @Override
    public Vo set(Long id) throws AdminException {
        UserScheduling userScheduling = dao.get(UserScheduling.class,id);
        if(userScheduling == null){
            throw new AdminException("排班信息异常");
        }
        if(UserScheduling.WorkStatus.on.name.toString().equals(userScheduling.getWorkStatus().name.toString())){
            userScheduling.setWorkStatus(UserScheduling.WorkStatus.off);
        }else {
            userScheduling.setWorkStatus(UserScheduling.WorkStatus.on);
        }
        userSchedulingRepository.save(userScheduling);
        return new Vo(HttpStatus.OK);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Vo save(List<UserScheduling> userSchedulings) throws AdminException {
        if(userSchedulings.size()>0){
            List<UserScheduling> list = userSchedulingRepository.findByDeletedFalseAndBossUserAndYearAndMonth(dao.get(BossUser.class,userSchedulings.get(0).getUserId()),userSchedulings.get(0).getYear(),userSchedulings.get(0).getMonth());
            if(list.size() > 0){
                userSchedulingRepository.deleteAll(list);
            }
            userSchedulings.forEach(userScheduling -> {
                if(userScheduling.getBossUser() == null){
                    userScheduling.setBossUser(dao.get(BossUser.class,userScheduling.getUserId()));
                }
            });
        }
        userSchedulingRepository.saveAll(userSchedulings);
        return new Vo(HttpStatus.OK);
    }

    @Override
    public Vo edit(List<UserScheduling> userSchedulings) throws AdminException {
        return null;
    }

}
